package org.aeon.client.view.components.sessionPanel;

import lombok.Setter;
import org.aeon.client.context.GlobalContext;
import org.aeon.entity.ChatMessage;
import org.aeon.entity.User;
import org.aeon.entity.dto.ChatGroupDto;
import org.aeon.entity.dto.GroupInfoDto;
import org.aeon.entity.dto.UserDto;
import org.aeon.entity.dto.UserInfoDto;
import org.aeon.message.request.ChatRequestMessage;
import org.aeon.message.request.GroupChatRequestMessage;
import org.aeon.service.UserMessageService;
import org.aeon.utils.SimpleRequestUtil;
import org.aeon.utils.SwingUtil;
import org.modelmapper.ModelMapper;

import javax.swing.*;
import java.awt.*;
import java.util.Date;

import static org.aeon.constant.MessageSendStatus.*;

/**
 * @author: MissingAeon
 * @date: 2024/2/20 19:13
 * @description:
 */
public class ChatEditorPanel extends JPanel {
    private final JTextArea textArea;
    private final JButton sendButton;
    @Setter
    private ChatRecordPanel recordPanel;

    public ChatEditorPanel(Dimension preferredSize) {
        SwingUtil.setSize(this, preferredSize);

        this.setLayout(null);

        // 创建文本域
        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setBackground(null);
        textArea.setFont(new Font("黑体", Font.PLAIN, 18));
        JScrollPane scrollPane = new JScrollPane(textArea);
        this.add(scrollPane);
        scrollPane.setBounds(10, 10, preferredSize.width - 20, 180);
        scrollPane.setBorder(null); // 去除边框
        scrollPane.getViewport().setBackground(null); // 设置背景颜色为空

        // 创建发送按钮
        sendButton = new JButton("发送");

        this.add(sendButton);
        sendButton.setBounds(preferredSize.width - 100, 200, 80, 30);

    }

    public void load(UserInfoDto user) {
        textArea.setText("");
        final GlobalContext context = GlobalContext.getContext();
        // 添加发送按钮的事件处理
        sendButton.addActionListener(e -> {
            final String sendText = textArea.getText();
            // TODO 文本校验
            if (sendText.isEmpty()) return;

            // 清空输入框
            textArea.setText("");

            // 先加入聊天记录
            final Date date = new Date();
            final ChatMessage tempMessage = new ChatMessage(sendText, date.getTime(), 0, user.getId());
            final ChatRecordItem recordItem = recordPanel.newNews(tempMessage, SENDING);
            recordPanel.setScrollBar(-1);
            // 发送信息
            final ModelMapper mapper = new ModelMapper();
            final ChatRequestMessage reqMsg = new ChatRequestMessage();
            final User currentUser = context.getCurrentUser();
            reqMsg.setMessage(mapper.map(currentUser, UserDto.class),
                    mapper.map(user, UserDto.class), sendText, date);

            SimpleRequestUtil.sendAndWaitOrTimeout(context, reqMsg, () -> {
                // 等待服务器返回
                recordItem.setStatus(NORMAL);
                // 服务器返回保存完整信息
                final ChatMessage messages = context.getUserChatMap().remove(reqMsg.getUid());
                UserMessageService.service.writeMessages(messages, currentUser.getId());
            }, () -> {
                // 超时保存未发送信息
                recordItem.setStatus(SEND_FAILED);
                // 聊天记录显示未发送成功
                UserMessageService.service.writeMessages(tempMessage, currentUser.getId());
            }, 2);
        });
    }

    public void load(GroupInfoDto group) {
        textArea.setText("");
        final GlobalContext context = GlobalContext.getContext();
        // 添加发送按钮的事件处理
        sendButton.addActionListener(e -> {
            final String sendText = textArea.getText();
            // TODO 文本校验
            if (sendText.isEmpty()) return;

            // 清空输入框
            textArea.setText("");

            // 先加入聊天记录
            final Date date = new Date();
            final ChatMessage tempMessage = new ChatMessage(sendText, date.getTime(), 0, -group.getId());
            final ChatRecordItem recordItem = recordPanel.newNews(tempMessage, SENDING);
            recordPanel.setScrollBar(-1);
            // 发送信息
            final ModelMapper mapper = new ModelMapper();
            final GroupChatRequestMessage reqMsg = new GroupChatRequestMessage();
            final User currentUser = context.getCurrentUser();
            reqMsg.setMessage(mapper.map(currentUser, UserDto.class),
                    mapper.map(group, ChatGroupDto.class), sendText, date);

            SimpleRequestUtil.sendAndWaitOrTimeout(context, reqMsg, () -> {
                // 等待服务器返回
                recordItem.setStatus(NORMAL);
                // 服务器返回保存完整信息
                final ChatMessage messages = context.getUserChatMap().remove(reqMsg.getUid());
                UserMessageService.service.writeMessages(messages, currentUser.getId());
            }, () -> {
                // 超时保存未发送信息
                recordItem.setStatus(SEND_FAILED);
                // 聊天记录显示未发送成功
                UserMessageService.service.writeMessages(tempMessage, currentUser.getId());
            }, 2);
        });
    }

}
